package email;

import hibernate.dao.HibernateConnection;
import hibernate.entity.Cliente;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.hibernate.Criteria;

public class EmailDAO {

//	QUERY -> Para gravar email do adminstrador
//	CREATE TABLE email (
//			idEmail INT(3) AUTO_INCREMENT,
//			email VARCHAR(40),
//			senha VARCHAR(40),
//			assunto VARCHAR(40),
//			texto VARCHAR(400),
//			servidor VARCHAR(40),
//			PRIMARY KEY (idEmail)
//			);
	
	private List<String> listaEmails;
	
	private DateFormat df;
	private Calendar c;
	private String dataAtual;
	
	private Connection con;
	private PreparedStatement stm;
	private ResultSet rs;
	
	public List<String> getListaEmail(){

		//HibernateConnection.buildSessionFactory();
		//session = HibernateConnection.openSession();			
		//Criteria criteria = session.createCriteria(Cliente.class);		
		listaEmails = new ArrayList<String>();

		try{
			//listaClientes = (List<Cliente>) criteria.list();
			con = getConnection();
			
			String sql = "SELECT Email FROM cliente WHERE Dt_nasc = date_format(now(),'%m-%d')";
			
			stm = con.prepareStatement(sql);
			
			rs = stm.executeQuery();
			while(rs.next()){
				listaEmails.add(rs.getString("email"));
			}
			
		} catch(Exception e){
			System.out.println("Erro: " + e);
		} finally {
			//HibernateConnection.closeSessionFactory();
			//session.clear();
			//session.close();
		}			
		return listaEmails;
	}
	
	public Email getDadosAdm(){
		Email emailAdm = new Email();
		
		try{
			con = getConnection();
			
			String sql = "SELECT * FROM email where idEmail = 1";
			
			stm = con.prepareStatement(sql);
			
			rs = stm.executeQuery();
			rs.next();
			
			emailAdm.setEmailDestinatario(rs.getString("email"));
			emailAdm.setSenhaRemetente(rs.getString("senha"));
			emailAdm.setAssunto(rs.getString("assunto"));
			emailAdm.setTexto(rs.getString("texto"));
			emailAdm.setServidor(rs.getString("servidor"));
			
			stm.close();
		} catch(Exception e){
			System.out.println("Erro: " + e);
		}
		return emailAdm;
	}
	
	public Connection getConnection() throws ClassNotFoundException, SQLException{
		Class.forName("com.mysql.jdbc.Driver");
    	return DriverManager.getConnection("jdbc:mysql://localhost/dbteste", "root", "root");
	}
}
